﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 弹出菜单
    /// </summary>
    public static class PopupMenu
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr MzPopupMenuCtor();

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzPopupMenuDispose(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzPopupMenuAppendMenuItem(IntPtr pObj, uint uIDNewItem, [MarshalAs(UnmanagedType.LPWStr)]string lpNewItem);

        [DllImport("MZFCWrapper.dll")]
        private static extern int MzPopupMenuMzTrackPopupMenu(IntPtr pObj, IntPtr hWnd, bool bUseCanelButton);
        #endregion

        const uint MZV2_MID_MIN = 0x0010;

        /// <summary>
        /// 显示弹出菜单
        /// </summary>
        /// <param name="cancel">是否显示取消按钮</param>
        /// <param name="buttons">菜单项集合</param>
        /// <returns>返回用户选择的菜单项索引。如果小于0，表示用户点击了取消。</returns>
        public static int Show(bool cancel, params string[] buttons)
        {
            IntPtr pObj = MzPopupMenuCtor();

            for (uint i = 0; i < buttons.Length; i++)
            {
                MzPopupMenuAppendMenuItem(pObj, MZV2_MID_MIN + i, buttons[i]);
            }

            int btn = MzPopupMenuMzTrackPopupMenu(pObj, IntPtr.Zero, cancel) - (int)MZV2_MID_MIN;

            MzPopupMenuDispose(pObj);

            return btn;
        }
    }
}
